import 'package:document_viewer_plugin/document_viewer_plugin.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';

import 'page_file_view.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

// ignore: public_member_api_docs
class HomePage extends StatefulWidget {
  // ignore: public_member_api_docs
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> files = <String>[
    'FileTest.docx',
    'FileTest.doc',
    'FileTest.xlsx',
    'FileTest.xls',
    'FileTest.pptx',
    'FileTest.ppt',
    'FileTest.pdf',
    'FileTest.txt',
    'FileTest.png',
    'FileTest.jpg',
    'FileTest.jpeg',
    'xzapp',
    'xzapp2',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('File View')),
      body: _buildBodyWidget(),
    );
  }

  Widget _buildBodyWidget() {
    return ListView.builder(
      itemCount: files.length,
      itemBuilder: (BuildContext context, int index) {
        if (files[index].startsWith('xzapp')) {
          return Container(
            margin: const EdgeInsets.only(top: 10),
            padding: const EdgeInsets.symmetric(horizontal: 15),
            child: ElevatedButton(
              onPressed: () {
                // launch('xz://class_join_page?classId=12345/*eol*/xzTab://tabName=mine');
                if (files[index] == 'xzapp') {
                  launchUrl(
                    Uri.parse('xz://app_launch/deeplink?link=xz%3A%2F%2Fclass_join_page%3FclassId%3D20241101011'),
                    mode: LaunchMode.externalApplication,
                  );
                } else {
                  launchUrl(
                    // Uri.parse('xz://class_join_page?classId=20241101011'),
                    // Uri.parse('xz://class_join_page?classId=20241101011/*eol*/xzTab://tabName=mine'),
                    // Uri.parse('xz://app_launch/deeplink?link=xz%3A%2F%2Fclass_join_page%3FclassId%3D20241101011%2F*eol*%2FxzTab%3A%2F%2FtabName%3Dmine'),
                    Uri.parse('xz://app_launch/deeplink?link=xzTab%3A%2F%2FtabName%3Dmine%2F*eol*%2Fxz%3A%2F%2Fclass_join_page%3FclassId%3D20241101011'),
                    // Uri.parse('xz://app_launch/deeplink?link=xzTab%3A%2F%2FtabName%3Dmine'),
                    // Uri.parse('xz://app_launch/deeplink?link=xz%3A%2F%2Fclass_join_page%3FclassId%3D20241101011'),
                    mode: LaunchMode.externalApplication,
                  );
                }
              },
              child: Text(files[index]),
            ),
          );
        }
        final String filePath = files[index];
        String fileShowText = '';

        final int i = filePath.lastIndexOf('/');
        if (i <= -1) {
          fileShowText = filePath;
        } else {
          fileShowText = filePath.substring(i + 1);
        }

        final Widget child = ElevatedButton(
          onPressed: () {
            FileViewController? controller;

            if (filePath.contains('http://') || filePath.contains('https://')) {
              controller = FileViewController.network(filePath);
            } else {
              controller = FileViewController.asset('assets/files/$filePath');
            }

            Navigator.of(context).push(
              MaterialPageRoute<void>(
                builder: (_) => FileViewPage(controller: controller!),
              ),
            );
          },
          child: Text(fileShowText),
        );

        return Container(
          margin: const EdgeInsets.only(top: 10),
          padding: const EdgeInsets.symmetric(horizontal: 15),
          child: child,
        );
      },
    );
  }
}
